package org.jhotdraw.samples.phototemplate;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfWriter;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.Stroke;
import java.awt.font.FontRenderContext;
import java.awt.font.TextLayout;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageTypeSpecifier;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.metadata.IIOInvalidTreeException;
import javax.imageio.metadata.IIOMetadata;
import javax.imageio.metadata.IIOMetadataNode;
import javax.imageio.stream.ImageOutputStream;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JProgressBar;
import org.imgscalr.Scalr;

/**
 *
 * @author Eliazar
 */
public class Teste {

    public void loadProgressBarExample() {
        new JProgressBarSetValue();
    }
    private static void teste1() {
        try {
            BufferedImage image = ImageIO.read(new File ("C:\\Users\\Eliazar\\Desktop\\PhotoShop\\FOTOS\\FUNDO FOTOS.jpg"));
            image = Scalr.resize(image, (int)UnitConv.mm2px(210, 90), (int)UnitConv.mm2px(297, 90), null);
//            Image newImage = image.getScaledInstance(597, 845, Image.SCALE_DEFAULT);            
//            ImageIO.write(ImageUtils.toBufferedImage(newImage), "png", new File("C:\\Users\\Eliazar\\Desktop\\t2.png"));
            ImageUtils.writeImagePNG(new File("C:\\Users\\Eliazar\\Desktop\\t3.png"), image, 90);
        } catch (IOException ex) {
            Logger.getLogger(Teste.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    public static void main(String[] args) throws Exception {
        teste1();
        //new Teste().loadProgressBarExample();
        
        //        WebServiceClient client = new WebServiceClient("http://www.radiografando.com.br/cetro/ordens_servicos/pullos/1436092b575e3826107a491f9377a30b/313011");        
//        System.out.println(client.getRecordMap());        

//        Color color = new Color(Integer.parseInt("-16711936"));
//          System.out.println(UnitConv.px2mm(200, 96));        
//          System.out.println(UnitConv.mm2px(255, 300));        
//          System.out.println(UnitConv.px2pt(793.7007993217649, 96));
//        System.out.println("incTomm:" + UnitConv.mm2px(210, 96));        
//        System.out.println("incTomm:" + UnitConv.mm2px(297, 96)); 
//        System.out.println("incTomm:" + UnitConv.px2px(793.7007993217649, 96, 300));        
//        System.out.println("incTomm:" + UnitConv.px2px(1122.5197018979247, 96, 300));
//        System.out.println("incTomm:" + UnitConv.px2px(789, 96, 300)); 
//        
//        double x1 = UnitConv.px2mm(960, 90);
//        
//        System.out.println("incTomm:" + UnitConv.px2mm(960, 90));                 
//        System.out.println("incTomm:" + UnitConv.mm2px(x1, 300));
//        System.out.println("incTomm:" + UnitConv.px2mm(UnitConv.mm2px(x1, 300), 300));
//        System.out.println("incTomm:" + UnitConv.px2px(960, 90, 300));

       /* BufferedImage image = new BufferedImage(800, 600, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2 = (Graphics2D) image.getGraphics();
        Shape rectangle = new Rectangle2D.Double(0D, 0D, 799, 599);
        g2.setColor(Color.WHITE);
        g2.fill(rectangle);
        Font font = new Font("Arial", Font.PLAIN, 25);
        
        FontMetrics metrics = g2.getFontMetrics(font);
        int height = metrics.getHeight();
        int width = metrics.stringWidth("Hello World!");
        
        float h2 = ((float)height)/96*300;
        float newFontSize = font.getSize() * h2 / height;
        font = font.deriveFont(newFontSize);
        g2.setColor(color);
        g2.setFont(font);
        FontRenderContext frc = g2.getFontRenderContext();
        TextLayout layout = new TextLayout("Hello World!", font, frc);
        
        
        
        g2.drawString("Hello World!", 100, 150);

        saveGridImage(new File("C:\\Users\\Eliazar\\Desktop\\aw.png"), image);*/
//        ImageIO.write(image, "png", new File("C:\\Users\\Eliazar\\Desktop\\aw1.png"));
//        System.out.println("W: " + width);
//        System.out.println("H: " + height);


//        int pixelPerInch=java.awt.Toolkit.getDefaultToolkit().getScreenResolution(); 
//        System.out.println("pixelPerInch: "+pixelPerInch);     
//        System.out.println(UnitConv.px2mm(793));
//        System.out.println(UnitConv.px2in(793, 96));
//        teste();
    }

    private static void teste() {
        try {
            String RESULT = "C:\\templates\\tttt.pdf";
            // step 1
            Document document = new Document(new Rectangle(200, 120));
            // step 2
            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(RESULT));
            // step 3
            document.open();
            // step 4
            PdfContentByte canvas = writer.getDirectContent();
            // state 1:
            canvas.setRGBColorFill(0xFF, 0x45, 0x00);
            // fill a rectangle in state 1
            canvas.rectangle(10, 10, 60, 60);
            canvas.fill();
            canvas.saveState();
            // state 2;
            canvas.setLineWidth(3);
            canvas.setRGBColorFill(0x8B, 0x00, 0x00);
            // fill and stroke a rectangle in state 2
            canvas.rectangle(40, 20, 60, 60);
            canvas.fillStroke();
            canvas.saveState();
            // state 3:
            canvas.concatCTM(1, 0, 0.1f, 1, 0, 0);
            canvas.setRGBColorStroke(0xFF, 0x45, 0x00);
            canvas.setRGBColorFill(0xFF, 0xD7, 0x00);
            // fill and stroke a rectangle in state 3
            canvas.rectangle(70, 30, 60, 60);
            canvas.fillStroke();
            canvas.restoreState();
            // stroke a rectangle in state 2
            canvas.rectangle(100, 40, 60, 60);
            canvas.stroke();
            canvas.restoreState();
            // fill and stroke a rectangle in state 1
            canvas.rectangle(130, 50, 60, 60);
            canvas.fillStroke();
            // step 5
            document.close();
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Teste.class.getName()).log(Level.SEVERE, null, ex);
        } catch (DocumentException ex) {
            Logger.getLogger(Teste.class.getName()).log(Level.SEVERE, null, ex);
        }
    }    

 private static void saveGridImage(File output, BufferedImage gridImage) throws IOException {
        output.delete();
        final String formatName = "png";
        for (Iterator<ImageWriter> iw = ImageIO.getImageWritersByFormatName(formatName); iw.hasNext();) {
            ImageWriter writer = iw.next();
            ImageWriteParam writeParam = writer.getDefaultWriteParam();
            ImageTypeSpecifier typeSpecifier = ImageTypeSpecifier.createFromBufferedImageType(BufferedImage.TYPE_INT_RGB);
            IIOMetadata metadata = writer.getDefaultImageMetadata(typeSpecifier, writeParam);
            if (metadata.isReadOnly() || !metadata.isStandardMetadataFormatSupported()) {
                continue;
            }

            setDPI(metadata, 300);

            final ImageOutputStream stream = ImageIO.createImageOutputStream(output);
            try {
                writer.setOutput(stream);
                writer.write(metadata, new IIOImage(gridImage, null, metadata), writeParam);
            } finally {
                stream.close();
            }
            break;
        }
    }

    private static void setDPI(IIOMetadata metadata, int dpi) throws IIOInvalidTreeException {          
        double dotsPerMilli = dpi / UnitConv.IN2MM;

        IIOMetadataNode horiz = new IIOMetadataNode("HorizontalPixelSize");
        horiz.setAttribute("value", Double.toString(dotsPerMilli));

        IIOMetadataNode vert = new IIOMetadataNode("VerticalPixelSize");
        vert.setAttribute("value", Double.toString(dotsPerMilli));

        IIOMetadataNode dim = new IIOMetadataNode("Dimension");
        dim.appendChild(horiz);
        dim.appendChild(vert);

        IIOMetadataNode root = new IIOMetadataNode("javax_imageio_1.0");
        root.appendChild(dim);

        metadata.mergeTree("javax_imageio_1.0", root);
    }
    


    private class JProgressBarSetValue extends JFrame {

        JProgressBar bar = new JProgressBar();
        JButton step = new JButton("Step");

        public JProgressBarSetValue() {
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            step.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    int value = bar.getValue() + 7;
                    if (value > bar.getMaximum()) {
                        value = bar.getMaximum();
                    }
                    bar.setValue(value);
                }
            });

            getContentPane().add(bar, BorderLayout.NORTH);
            getContentPane().add(step, BorderLayout.EAST);
            pack();
            setVisible(true);
        }
    }
}
